package com.googlecode.jmicroerp.model;

import com.googlecode.jmicroerp.security.SpringSubject;
import com.googlecode.jmicroerp.wicket.SecuredWebSession;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.MappedSuperclass;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import javax.persistence.Version;
import org.joda.time.DateTime;

/**
 *
 * @author alex
 */
@MappedSuperclass
public abstract class AbstractEntitate implements Entitate {

    /**
     *
     */
    private static final long serialVersionUID = -3375219535375347822L;
    private Long id;
    private Utilizator creatDe;
    private Date dataCreare;
    private Utilizator modificatDe;
    private Date dataModificare;
    private long versiune;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "ID")
    @Override
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Column(name = "VERSIUNE")
    @Version
    @Override
    public long getVersiune() {
        return versiune;
    }

    public void setVersiune(long versiune) {
        this.versiune = versiune;
    }

    @Temporal(value = TemporalType.TIMESTAMP)
    @Column(name = "DATA_CREARE")
    public Date getDataCreare() {
        return dataCreare;
    }

    public void setDataCreare(Date dataCreare) {
        this.dataCreare = dataCreare;
    }

    
    @Temporal(value = TemporalType.TIMESTAMP)
    @Column(name = "DATA_MODIFICARE")
    public Date getDataModificare() {
        return dataModificare;
    }

    public void setDataModificare(Date dataModificare) {
        this.dataModificare = dataModificare;
    }

    @Override
    @ManyToOne(fetch = FetchType.LAZY, targetEntity = Utilizator.class)
    @JoinColumn(name = "CREAT_DE")
    public Utilizator getCreatedBy() {
        return creatDe;
    }

    @Override
    public void setCreatedBy(Utilizator u) {
        creatDe = u;
    }

    @Override
    @Transient
    public DateTime getCreatedDate() {
        if (dataCreare == null) {
            return null;
        }
        return new DateTime(dataCreare.getTime());
    }

    @Override
    public void setCreatedDate(DateTime dt) {
        dataCreare = dt.toDate();
    }

    @Override
    @ManyToOne(fetch = FetchType.LAZY, targetEntity = Utilizator.class)
    @JoinColumn(name = "MODIFICAT_DE")
    public Utilizator getLastModifiedBy() {
        return modificatDe;
    }

    @Override
    public void setLastModifiedBy(Utilizator u) {
        modificatDe = u;
    }

    @Override
    @Transient
    public DateTime getLastModifiedDate() {
        if (dataModificare == null) {
            return null;
        }
        return new DateTime(dataModificare.getTime());
    }

    @Override
    public void setLastModifiedDate(DateTime dt) {
        dataModificare = dt.toDate();
    }

    @Override
    @Transient
    public boolean isNew() {
        return (id == null);
    }
    
    @PrePersist
    protected void prePersist() {
        this.dataCreare = new Date();
        this.creatDe = getUserPrincipal();
    }

    @PreUpdate
    protected void preUpdate() {
        this.dataModificare = new Date();
        this.modificatDe = getUserPrincipal();
    }
    
    @Transient
    private Utilizator getUserPrincipal() {
        SecuredWebSession webSession = SecuredWebSession.get();
        SpringSubject subject = null; 
        Utilizator utilizator = null;
        if (webSession != null) {
            subject = webSession.getUserDetail();
        }
        if (subject != null) {
            utilizator = subject.getUtilizator();
        }
        return utilizator;
    }
}
